package com.bw.day10.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.bw.day10.entity.Organization;
import com.bw.day10.entity.Paybill;
import com.bw.day10.entity.User;
import com.bw.day10.enums.TypeEnums;
import com.bw.day10.service.PaybillService;
import com.carrey.carrey_utils.FileUtils;
import com.carrey.carrey_utils.RandomUtils;
import com.github.pagehelper.PageInfo;

@Controller
public class PaybillController {
	@Autowired
	private PaybillService paybillService;
	@RequestMapping("list")
	public String getlist(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize,Model model,Paybill paybill) {
		PageInfo<Paybill> info = paybillService.getlist(pageNum,pageSize,paybill);
		model.addAttribute("info",info);
		List<Organization> organizations = paybillService.getorganization();
		model.addAttribute("organizations",JSON.toJSON(organizations));
		model.addAttribute("paybill", paybill);
		return "list";
	}
	@RequestMapping("update")
	public String update(Model model,Integer id) {
		if (id!=null && id!=0) {
			Paybill paybill = paybillService.selectid(id);
			model.addAttribute("paybill",paybill);
		}
		List<Organization> organizations = paybillService.getorganization();
		model.addAttribute("organizations",JSON.toJSON(organizations));
		TypeEnums[] values = TypeEnums.values();
		model.addAttribute("typeenus",values);
		ArrayList<String> arrayList = new ArrayList<String>();
		for (TypeEnums typeenum : values) {
			arrayList.add(typeenum.getValue());
		}
		model.addAttribute("typeen",JSON.toJSON(arrayList));
		return "update";
	}
	@RequestMapping("getorganization")
	@ResponseBody
	public List<Organization> getorganization() {
		List<Organization> organizations = paybillService.getorganization();
		return organizations;
	}
	@RequestMapping("toupdate")
	public String toupdate(Model model,Paybill paybill,MultipartFile file) throws IllegalStateException, IOException {
		if (!file.isEmpty()) {
			String originalFilename = file.getOriginalFilename();
			String uuid = RandomUtils.getUuid();
			String fileExtensions = FileUtils.getFileExtensions(originalFilename);
			File file2 = new File("E:/imgpic/"+uuid+fileExtensions);
			file.transferTo(file2);
			paybill.setBills("/imgpic/"+uuid+fileExtensions);
		}
		System.out.println(paybill+"============================");
		paybillService.inserpaybill(paybill);
		return "redirect:list";
	}
	@RequestMapping("deletes")
	public String deletes(Integer id) {
		paybillService.deletes(id);
		return "redirect:list";
	}
	@RequestMapping("tologin")
	public String tologin() {
		return "login";
	}
	@RequestMapping("login")
	public String login(User user,HttpServletRequest request) {
		User user2 = paybillService.login(user);
		if (user2!=null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user2);
			return "redirect:list";
		}
		return "login";
	}
}
